//
//  UIBarButtonItem+Extension.swift
//  weibo.chb
//
//  Created by 褚红彪 on 16/1/21.
//  Copyright © 2016年 chuhongbiao. All rights reserved.
//

import UIKit
let itemSizeFont : CGFloat = 14.0
extension UIBarButtonItem{
    //这样写便利构造器如果某个参数没有可以不传
    convenience init(imgName: String? = nil, title: String? = nil, target: AnyObject?, action: Selector){
        self.init()
        let Btn = UIButton()
        if let imgStr = imgName {
            Btn.setImage(UIImage(named: imgStr), forState: UIControlState.Normal)
            Btn.setImage(UIImage(named: "\(imgStr)_highlighted"), forState: UIControlState.Highlighted)
        }
        if let str = title{
            Btn.setTitle(str, forState: UIControlState.Normal)
            Btn.setTitle(str, forState: UIControlState.Highlighted)
            Btn.setTitleColor(UIColor(white: 80 / 255.0, alpha: 1.0), forState: UIControlState.Normal)
            Btn.setTitleColor(UIColor(white: 150 / 255.0, alpha: 1.0), forState: UIControlState.Highlighted)
            Btn.titleLabel?.font = UIFont.systemFontOfSize(itemSizeFont)
        }
        Btn.sizeToFit()
        Btn.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        customView = Btn
    }
}
